export const SUBJECT = new Set(['语文', '数学', '英语', '物理', '化学', '生物']);

export class Student {
  name: string = '';
  age: number = 18;
  sex: string = '';
  isBoarder: boolean = true;
  transcript: Map<string, number> = new Map<string, number>();

  constructor(name: string, age: number, sex: string, isBoarder: boolean) {
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.isBoarder = isBoarder;
  }

  introduce(): string {
    let boarderStatus: string = this.isBoarder ? '住宿生' : '走读生';
    return `姓名${this.name},今年${this.age}岁,是一名${boarderStatus}`;
  }

  setSubjectScore(subject: string, score: number): void {
    if (score < 0 && score > 100) {
      throw new Error();
    }
    this.transcript.set(subject, score);
  }
}

export function calTotalScore(student: Student) {
  let total: number = 0;
  for (let score of student.transcript.values()) {
    total += score;
  }
  return total;
}

export function calAverageScore(totalScore: number, subjectCount: number) {
  if (subjectCount === 0) {
    throw new Error();
  }
  let averageScore: number = totalScore / subjectCount;
  let averageScoreStr: string = String(Math.round(averageScore * 100) / 100);
  return averageScoreStr;
}

// 7.异常处理
export let referenceErr: ReferenceError = new ReferenceError("reference error");